<template>
  <div class="my-container">
    <!-- 登录部分的理解 -->
    <div class="header is-login" v-if="user">
      <div class="login-btn">
        <div class="left">
          <van-image class="login-logo" round :src="userInfo.photo" />
          <span class="name">{{userInfo.name}}</span>
        </div>
        <div class="right">
          <van-button round size="mini" type="info" plain>编辑资料</van-button>
        </div>
      </div>

      <div class="data-stats">
        <div class="data-item">
          <span class="count">{{userInfo.art_count}}</span>
          <span class="text">头条</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.follow_count}}</span>
          <span class="text">关注</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.fans_count}}</span>
          <span class="text">粉丝</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.like_count}}</span>
          <span class="text">获赞</span>
        </div>
      </div>
    </div>

    <!-- 未登录 -->
    <div class="header not-login" @click="$router.push('/login')" v-else>
      <div class="login-btn">
        <img src="~@/assets/mobile.png" class="mobile-img" alt />
        <span class="text">登录 / 注册</span>
      </div>
    </div>

    <!-- 消息通知&小智同学 -->
    <van-cell-group>
      <van-cell title="消息通知" is-link />
      <van-cell title="小智同学" is-link />
      <van-cell title="退出登录" class="logout" v-if="user" @click="logout" />
    </van-cell-group>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfo } from '@/api/user.js'
export default {
  name: 'my',
  data() {
    return {
      // 用来存储获取到的用户信息
      userInfo: {}
    }
  },
  created() {
    this.loadUserInfo()
  },
  methods: {
    // 退出操作
    logout() {
      this.$dialog
        .confirm({
          message: '确认退出码？'
        })
        .then(() => {
          // on confirm
          // 确认退出我们就要将token清空
          this.$store.commit('setUser', null)
        })
        .catch(() => {
          // on cancel
        })
    },
    // 获取用户数据
    async loadUserInfo() {
      const { data } = await getUserInfo()
      this.userInfo = data.data
    }
  },
  computed: {
    ...mapState(['user'])
  }
}
</script>

<style lang="less" scoped>
.my-container {
  .header {
    height: 361px;
    background-image: url(~@/assets/banner.png);
    background-size: cover;
  }

  // 未登录部分的样式
  .not-login {
    display: flex;
    align-items: center;
    justify-content: center;
    .login-btn {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      .mobile-img {
        width: 132px;
        height: 132px;
        margin-bottom: 15px;
      }
      .text {
        font-size: 28px;
        color: #fff;
      }
    }
  }

  // 消息通知，小智同学部分的样式
  .van-cell-group {
    background-color: #f5f7f9;
  }

  .logout {
    margin-top: 10px;
    text-align: center;
    color: red;
  }

  // 已登录部分的内容
  .is-login {
    .login-btn {
      height: 231px;
      padding: 76px 32px 23px;
      box-sizing: border-box;
      display: flex;
      align-items: center;
      justify-content: space-between;
      .left {
        display: flex;
        align-items: center;
        .login-logo {
          width: 132px;
          height: 132px;
          border: 4px solid #fff;
          margin-right: 23px;
        }
        .name {
          font-size: 30px;
          color: #fff;
        }
      }
    }
    .data-stats {
      display: flex;
      .data-item {
        height: 130px;
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        color: #fff;
        .count {
          font-size: 36px;
        }
        .text {
          font-size: 23px;
        }
      }
    }
  }
}
</style>
